import unittest


class TestStringMethods(unittest.TestCase):

    """
    说明:
        1. tuple类型也支持切片操作;
        2. 创建单个数值的tuple时, 必须以`tuple2 = (5,)`而不能以`no_tuple2 = (5)`这种形式, 逗号不可省略.
        3. tuple类型一旦创建不可再更改, 否则会抛出异常TypeError: 'tuple' object does not support item assignment
    """
    def test_create(self):
        tuple1 = (1, 2, 3, 4)
        print(tuple1[0:])
        # 这种方式创建出的并不是元组, 而是int
        no_tuple2 = (5)
        tuple2 = (5,)
        print(type(no_tuple2))
        print(type(tuple2))
        # 下面的这行代码会报错TypeError: 'tuple' object does not support item assignment
        # tuple2[3] = 23

    """
    功能: 元组的运算符
    """
    def test_tuple_operation(self):
        tuple1 = (1, 2, 3, 4)
        # 获取元祖长度
        print(len(tuple1))
        # 使元组复制一次
        print(tuple1 * 2)
        # 元组相加
        print(tuple1 + tuple1)

    """
    功能: in操作符检测元素是否在元组中
    """
    def test_tuple_check(self):
        tuple1 = (1, 2, 3, 4)
        print(2 in tuple1)
        print(21 in tuple1)

    """
    功能: 元组的迭代
    """
    def test_foreach(self):
        tuple1 = (1, 2, 3, 4)
        for x in tuple1:
            print(x)

    """
    说明:
        1. 可以使用tuple函数将一个list转换为元组;
    """
    def test_convert_list_2_tuple(self):
        list1 = [1, 2, 3]
        tuple1 = tuple(list1)
        print(type(tuple1))
        print(tuple1)

    def test_sub_method(self):
        list1 = [1, 2, 3]
        print(max(list1))
        print(min(list1))


if __name__ == '__main__':
    unittest.main()
